﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace XHCTMS.VIEW
{
    public partial class Logined : System.Web.UI.Page
    {
        string[] parasValue = new string[] { };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                parasValue =new string[] { ddlname.Text, txtTime.Value };
                Session["SearchArr"] = parasValue;
                Session["Page"] = 1;
                //Session["PageSize"] = 5;
                Session["MaxPage"] = new BLL.logined().Amount(parasValue);
              //  this.btnPrevious.Enabled = false;
                GetView(new BLL.logined().Search(parasValue),int.Parse(Session["MaxPage"].ToString()));


                GetTxtName();
            }
        }

        private void GetView(DataTable dt ,int maxPage)
        {
            List<string> pageList = new List<string> { };
            for (int i = 1; i <= maxPage; i++)
            {
                pageList.Add(i.ToString() + "/" + maxPage.ToString());
            }
            this.ddlPage.DataSource = pageList;
            this.ddlPage.DataBind();
            this.ddlPage.SelectedIndex = int.Parse(Session["Page"].ToString()) - 1;
            this.Logininfo.DataSource = dt;
            this.Logininfo.DataBind();
            //int pageSize = Convert.ToInt32(Session["PageSize"]);
            //int pageIndex = Convert.ToInt32(Session["PageIndex"]);
            //DataTable dt = new XHCTMS.BLL.logined().Paging(pageSize, pageIndex);
            //this.Logininfo.DataSource = dt;
            //Logininfo.DataBind();
        }

        private int ChangePageSize()
        {
            return Convert.ToInt32(Session["PageSize"]);
        }
        private int ChangePageIndex()
        {
            return Convert.ToInt32(Session["PageIndex"]);
        }
        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int count = int.Parse(Session["page"].ToString()) - 1;
            if (count > 0)
            {
                GetView(new BLL.logined().Search(parasValue, count), int.Parse(Session["maxPage"].ToString()));
                Session["page"] = count;
                this.ddlPage.SelectedIndex = count - 1;
            }


            //int pageSize = ChangePageSize();
            //int pageIndex = ChangePageIndex() - 1;
            //this.btnPrevious.Enabled = true;
            //this.btnNext.Enabled = true;
            //Session["PageIndex"] = pageIndex;
            //DataTable dt = new XHCTMS.BLL.logined().Paging(pageSize, pageIndex);
            //this.Logininfo.DataSource = dt;
            //Logininfo.DataBind();
            //if (pageIndex == 1)
            //{
            //    this.btnPrevious.Enabled = false;
            //}
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {

            parasValue = (string[])Session["SearchArr"];
            int page = int.Parse(Session["Page"].ToString()) + 1;
            if (page <= int.Parse(Session["MaxPage"].ToString()))
            {
                GetView(new BLL.logined().Search(parasValue, page), int.Parse(Session["MaxPage"].ToString()));
                Session["Page"] = page;
                this.ddlPage.SelectedIndex = page - 1;
            }
            //int pageSize = ChangePageSize();
            //int pageIndex = ChangePageIndex() + 1;
            //int maxPage = Convert.ToInt32(Session["MaxPage"]);
            //this.btnNext.Enabled = true;
            //this.btnPrevious.Enabled = true;
            //Session["PageIndex"] = pageIndex;
            //DataTable dt = new XHCTMS.BLL.logined().Paging(pageSize, pageIndex);
            //this.Logininfo.DataSource = dt;
            //Logininfo.DataBind();
            //if (pageIndex == maxPage)
            //{
            //    this.btnNext.Enabled = false;
            //}
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            GetView(new BLL.logined().Search(parasValue), int.Parse(Session["MaxPage"].ToString()));
            Session["page"] = 1;
            this.ddlPage.SelectedIndex = 0;     
            //int pageSize = ChangePageSize();
            //int pageIndex = 1;
            //Session["PageIndex"] = pageIndex;
            //this.btnPrevious.Enabled = false;
            //this.btnNext.Enabled = true;
            //DataTable dt = new XHCTMS.BLL.logined().Paging(pageSize, pageIndex);
            //this.Logininfo.DataSource = dt;
            //Logininfo.DataBind();
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int maxPage = int.Parse(Session["MaxPage"].ToString());
            GetView(new BLL.logined().Search(parasValue, maxPage), maxPage);
            Session["Page"] = maxPage;
            this.ddlPage.SelectedIndex = maxPage - 1;   

            //int pageSize = ChangePageSize();
            //int pageIndex = Convert.ToInt32(Session["MaxPage"]);
            //Session["PageIndex"] = pageIndex;
            //this.btnPrevious.Enabled = true;
            //this.btnNext.Enabled = false;
            //DataTable dt = new XHCTMS.BLL.logined().Paging(pageSize, pageIndex);
            //this.Logininfo.DataSource = dt;
            //Logininfo.DataBind();
        }

        /// <summary>
        /// 获取下拉框的值
        /// </summary>
        private void GetTxtName()
        {
            DataTable dtname = new DAL.logined().GetUserName();
            this.ddlname.Items.Clear();
            this.ddlname.DataSource = dtname;
            this.ddlname.DataTextField = "user_name";
            this.ddlname.DataValueField = "user_id";
            this.ddlname.DataBind();
            ddlname.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {           
           
            //MODEL.logined login = new MODEL.logined();
            //if (!string.IsNullOrEmpty(ddlname.Text))
            //{
            //    login.LoginUserId = Convert.ToInt32(this.ddlname.Text);
            //}
            //if (!string.IsNullOrEmpty(txtTime.Value))
            //{                
            //    login.LoginTime = this.txtTime.Value;
            //}

            //DataTable dt = new XHCTMS.BLL.logined().SelecteBy(login);
            try
            {
                parasValue = new string[] { ddlname.Text, txtTime.Value };
                Session["SearchArr"] = parasValue;
                Session["MaxPage"] = new BLL.logined().Amount(parasValue);
                Session["Page"] = 1;
                GetView(new BLL.logined().Search(parasValue), int.Parse(Session["MaxPage"].ToString()));
            }
            catch
            {

            }
         


            //this.Logininfo.DataSource = dt;
            //this.Logininfo.DataBind();
            
        }

        protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            parasValue = (string[])Session["SearchArr"];
            int page = int.Parse(this.ddlPage.SelectedValue.Split('/')[0]);
            GetView(new BLL.logined().Search(parasValue, page), int.Parse(Session["maxPage"].ToString()));
            Session["page"] = page;
            this.ddlPage.SelectedIndex = page - 1;
        }
    }
}